from django.shortcuts import render,reverse
from django.http import HttpResponse,JsonResponse,Http404,HttpResponseRedirect
from myadmin import models
from django.contrib.auth.hashers import make_password,check_password
from .IndexViews import gettypes
from django.views.decorators.csrf import csrf_exempt
from .PayViews import pay_request

import random,requests
# Create your views here.

# 订单结算页
def confirm(request):

    #接收当前的购物车商品信息
    ids = request.GET.get('ids','').split(',')
    # 获取对应的购物车商品对象
    data=models.Cart.objects.filter(id__in=ids)
    print(data)
    # 分配数据
    context = {'booktypes':gettypes(),'carts':data}

    return render(request,'myhome/order/confirm.html',context)


#创建订单
def create(requeat):
    # 接收数据
    data=requeat.POST.dict()
    data.pop('csrfmiddlewaretoken')
    VipUser=requeat.session.get('VipUser')

    # 计算总价
    totalprice=0
    #查询购物车商品
    ids=data['ids'].split(',')
    carts=models.Cart.objects.filter(id__in=ids)
    for x in carts:
        totalprice += x.num * x.bid.price

    #准备订单数据
    orderdata={
        'uid':models.Users.objects.get(id=VipUser['id']),
        'name':data['name'],
        'phone':data['phone'],
        'address':data['address'],
        'totalprice':float(round(totalprice,2))
    }

    #执行订单的创建
    orderobj=models.Order(**orderdata)
    orderobj.save()

    # 准备订单详情数据
    for x in carts:
        detaildata={
            'orderid':orderobj,
            'bid':x.bid,
            'price':x.bid.price,
            'num':x.num
        }
        # 执行订单详情的创建
        detailobj=models.OrderDetail(**detaildata)
        detailobj.save()

        # 把下订单的购物车商品，在购物车中删除
        x.delete()

    # 跳转到订单支付页面
    url=reverse('myhome_order_pay',args=(orderobj.id,))
    return HttpResponseRedirect(url)

# 发起支付请求 buy+
def buypay(request,orderid):
    # 获取订单对象
    obj = models.Order.objects.get(id=orderid)
    return pay_request(obj)
